package com.glela.mq.service.impl;

import javax.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.stereotype.Service;
import com.glela.mq.service.OrderQueueService;
import com.zls.wx.message.CommissionArrivalMsg;
import com.zls.wx.message.MarketingSuccessMsg;
import com.zls.wx.message.enums.ExchangeEnum;
import com.zls.wx.message.enums.RoutingKey;

@Service
public class OrderQueueServiceImpl implements OrderQueueService {
	private static Logger logger = LoggerFactory.getLogger(OrderQueueServiceImpl.class);

	@Resource(name = "direcTemplates")
	private AmqpTemplate	direcTemplates;
	@Resource(name = "openStoreDoneTemplates")
	private AmqpTemplate	openStoreDoneTemplates;
	@Resource(name = "payCallBackTemplates")
	private AmqpTemplate	payCallBackTemplates;
	/*@Resource(name = "messageSyncTemplates")
	private AmqpTemplate	messageSyncTemplates;*/

	@Override
	public void createCommision(Message OrderSn ) throws Exception {
		 direcTemplates.send("waitingCommisionKeys", OrderSn);
	}
	@Override
	public void createOrder(String orderContext) throws Exception {
		direcTemplates.convertAndSend("directKeys", orderContext);
	}

	@Override
	public void createOrder(Object orderContext) throws Exception {
		direcTemplates.convertAndSend("directKeys", orderContext);

	}

	@Override
	public void addMQPayCallBack(Message commisionInfo) throws Exception {
		payCallBackTemplates.send(commisionInfo);
	}

	/**
	 * 序列处理商品详情页的缓存
	 */
	@Override
	public void updateGoodsInfoRedisCache(Object params) throws Exception {
		direcTemplates.convertAndSend("updateGoodsInfoRedisCache", params);
	}

	@Override
	public void sendWxTemplateMsgMarketingSuccess(MarketingSuccessMsg msg) {
		try {
			direcTemplates.convertAndSend(ExchangeEnum.DIRECT_YG_ORDER, RoutingKey.YG_ORDER_MARKETING_SUCCESS_NOTIFY_STORE_WX, msg);
		} catch (AmqpException e) {
			logger.error("发送模版消息失败：{}", e.getMessage(), e);
		}
	}

	@Override
	public void sendWxTemplateMsgCommissionArrival(CommissionArrivalMsg msg) {
		try {
			direcTemplates.convertAndSend(ExchangeEnum.DIRECT_YG_ORDER, RoutingKey.YG_ORDER_MARKETING_ARRIVAL_NOTIFY_STORE_WX, msg);
		} catch (AmqpException e) {
			logger.error("发送模版消息失败：{}", e.getMessage(), e);
		}
	}

	/*@Override
	public void messageSync(Object o) throws Exception {
		messageSyncTemplates.convertAndSend("messageSync", o);
	}*/

	@Override
	public void openStoreDone(Object o) throws Exception {
		openStoreDoneTemplates.convertAndSend("openStoreDone", o);
	}

	@Override
	public void calculationEarnings(String orderSn) throws Exception {
		direcTemplates.convertAndSend("calculateEarning", orderSn);
	}
}